Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseURL: 'http://43.139.97.111:8080',
    allOrderList: [],
    unPayOderList: [],
    userInfo: {},
    tabs: [
      {
        id: 1,
        name: '全部订单'
      },
      {
        id: 2,
        name: '待付款'
      },
      {
        id: 3,
        name: '退款/售后'
      }
    ],
    tabIndex: 0

  },
  selectedTab(e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      tabIndex: index
    })
    this.getAllOrder()
  },

  getAllOrder() {
    var _this = this;
    var uid = _this.data.userInfo.userId;
    let temp = [];
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: 'http://43.139.97.111:8080/orderItem/queryByUserId?userId=' + uid,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data.data);
        if (res.statusCode == 200) {
          _this.setData({
            allOrderList: res.data.data,
          })
          let j = 0
          for (let i = 0; i < _this.data.allOrderList.length; i++) {
            if (_this.data.allOrderList[i].orderPay == 0) {
              temp[j] = _this.data.allOrderList[i]
              j++
            }
          }
        }
        setTimeout(() => {
          _this.setData({
            unPayOderList: temp
          })
        }, 200)
        wx.hideLoading()
      }
    })

  },
  //删除订单
  deleteOrder(e) {
    var _this = this;
    const index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '您确定要删除吗？',
      success: function (res) {
        if (res.confirm) {
          //请求删除购物车数据
          wx.request({
            url: 'http://43.139.97.111:8080/orderItem/deleteById?id=' + _this.data.allOrderList[index].orderId,
            header: {
              'content-type': 'application/json' // 默认值
            },
            method: 'DELETE',
            success(res) {
              console.log(res.data);
              if (res.statusCode == 200) {
                wx.showToast({
                  title: '成功',
                  icon: 'success',
                  duration: 2000
                })
                _this.getAllOrder()
              }
            }
          })
        }
      }
    })

  },
  //删除订单
  deleteUnPayOrder(e) {
    var _this = this;
    const index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '您确定要删除吗？',
      success: function (res) {
        if (res.confirm) {
          //请求删除购物车数据
          wx.request({
            url: 'http://43.139.97.111:8080/orderItem/deleteById?id=' + _this.data.unPayOderList[index].orderId,
            header: {
              'content-type': 'application/json' // 默认值
            },
            method: 'DELETE',
            success(res) {
              console.log(res.data);
              if (res.statusCode == 200) {
                wx.showToast({
                  title: '成功',
                  icon: 'success',
                  duration: 2000
                })
                _this.getAllOrder()
              }
            }
          })
        }
      }
    })

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var _this = this;
    const userinfo = wx.getStorageSync("userInfo");
    this.setData({
      userInfo: userinfo
    });
    console.log(_this.data.userInfo.userId)
    if (_this.data.userInfo == '') {
      wx.showToast({
        title: '请先登录',
        icon: 'success',
        duration: 2000
      })
    } else {
      this.getAllOrder()
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var _this = this;
    const userinfo = wx.getStorageSync("userInfo");
    this.setData({
      userInfo: userinfo
    });
    console.log(_this.data.userInfo.userId)
    if (_this.data.userInfo == '') {
      wx.showToast({
        title: '请先登录',
        icon: 'success',
        duration: 2000
      })
    } else {
      this.getAllOrder()
    }


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})